package com.ssy.lingxi.workflow.config;

import com.ssy.lingxi.workflow.service.IProcessDeployService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.spring.boot.starter.event.PostDeployEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 服务启动时，解析每个流程图
 * @author 万宁
 * @version 2.0.0
 * @date 2021-04-21
 */
@Configuration
public class ProcessConfig {
    @Resource
    private RepositoryService repositoryService;

    @Resource
    private IProcessDeployService processDeployService;

    //临时变量：使用新接口的流程列表
    //当所有流程改造完毕，要删除此列表
    private final List<String> newProcessKeyList = Stream.of(
            "platform_member_verify_confirm",
            "platform_member_verify",
            "member_depository_one",
            "member_depository_three",
            "member_depository_four",
            "member_depository_five",
            "member_depository_six",
            "member_depository_seven",
            "member_detail_update_one",
            "member_detail_update_two",
            "member_detail_update_three",
            "order_direct_delivery",
            "order_pay_one_deliveries",
            "order_pay_deliveries",
            "order_validate_pay_seven_deliveries",
            "order_right_point",
            "order_requisition",
            "order_pay_online_deliveries",
            "order_delivery_plan",
            "order_delivery_notice_order",
            "need_plan_inner_verify",
            "purchase_plan_inner_verify",
            "pno_outer_verify_ext",
            "pno_manual_delivery_outer_verify_ext",
            "pno_supplier_inner_verify",
            "pno_process_inner_verify",
            "granting_credit_apply_verify",
            "contract_apply_amount_verify",
            "contract_outer_verify_ext",
            "contract_party_a_inner_verify",
            "contract_party_b_inner_verify",
            "after_sale_repair_inner_verify",
            "after_sale_replace_goods_inner_verify",
            "after_sale_replace_goods_contract_verify_ext",
            "after_sale_replace_goods_contract_manual_delivery_ext",
            "after_sale_replace_goods_manual_delivery_ext",
            "after_sale_replace_goods_outer_verify_ext",
            "after_sale_return_goods_inner_verify",
            "after_sale_return_goods_contract_verify_ext",
            "after_sale_return_goods_contract_manual_delivery_ext",
            "after_sale_return_goods_outer_verify_ext",
            "after_sale_return_goods_manual_delivery_ext",
            "trade_inquiry_inner",
            "trade_inquiry_external_ext",
            "trade_quote_inner",
            "trade_confirm_quote_inner",
//            "trade_offer_inner_procedure",
            "purchase_order_inner_verify",
            "purchase_order_sale_inner_verify",
            "purchase_order_purchase_add_order_ext",
            "purchase_order_outer_verify_repeat_pay_delivery_ext",
            "purchase_order_outer_verify_one_ext",
            "purchase_order_outer_verify_two_ext",
//            "product_pricing_outer_order_ext",
//            "product_pricing_inner_order",
//            "product_confirm_pricing_inner_order",
            "purchase_offer_inner_verify",
            "purchase_confirm_offer_inner_verify",
            "purchase_inquiry_inner_verify",
            "purchase_inquiry_outer_verify_ext",
            "purchase_bidding_interior_outer_verify",
            "purchase_bidding_examine_interior_outer_verify",
            "purchase_bidding_external_outer_verify_ext",
            "purchase_invite_tender_ext",
            "purchase_add_invite_tender",
            "purchase_submit_tender",
            "purchase_finish_tender",
            "member_appraisal_verify",
            "member_rectify_verify_ext",
            "logistics_order_verify_ext",
            "platform_activity_outer_verify_ext",
            "platform_activity_inner_verify",
            "platform_activity_sign_up_inner_verify",
            "merchant_activity_outer_verify_ext",
            "merchant_activity_inner_verify",
            "merchant_coupon_verify",
            "platform_coupon_verify",
            "after_sale_return_goods_outer_verify_offline_logistics_ext",
            "after_sale_return_goods_outer_verify_contract_offline_logistics_ext",
            "after_sale_replace_goods_outer_verify_offline_logistics_ext",
            "purchase_requisition_inner_verify",
            "after_sale_return_goods_pf_manual_delivery_ext",
            "order_pay_online_deliveries",
            "order_commerce_import",
            "settle_accounts_business_reconciliation_ext",
            "product_service_materials_add_level_0",
            "product_service_materials_add_level_1",
            "product_service_materials_add_level_2",
            "product_service_materials_change_or_enable_level_0",
            "product_service_materials_change_or_enable_level_1",
            "product_service_materials_change_or_enable_level_2"
    ).collect(Collectors.toList());

    @EventListener
    public void onPostDeploy(PostDeployEvent event) {
        //删除在Resource目录下没有对应bpmn的流程定义
        //processDeployService.deleteDeploymentWithoutBpmnFile();

        //获取流程最新版本定义
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().latestVersion().list();
        for (ProcessDefinition processDefinition : processDefinitionList) {
            //当所有流程改造完毕后，删除此段代码
            if(!newProcessKeyList.contains(processDefinition.getKey())) {
                continue;
            }

            System.out.println("开始解析流程：" + processDefinition.getResourceName() + " => " + processDefinition.getKey());

            //获取模型
            BpmnModelInstance model = repositoryService.getBpmnModelInstance(processDefinition.getId());

            String errorMsg = processDeployService.checkProcessModel(processDefinition, model);
            if(StringUtils.hasLength(errorMsg)) {
                System.err.println(errorMsg);
                System.exit(1);
            }

            //自定义的表保存流程任务等信息
            processDeployService.deployProcess(processDefinition, model);
            System.gc();
        }
    }
}
